package data;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

public class GetOrderListResponse extends Response {
	private List<Order> mOrders;
	
	public GetOrderListResponse(int exitCode, Order[] orders) {
		mExitCode = exitCode;
		mOrders = new ArrayList<>();
		if (orders != null)
			for (Order o : orders)
				mOrders.add(o);
	}
	
	public List<Order> getOrders() {
		return mOrders;
	}

	public static final JsonDeserializer<GetOrderListResponse> deserializer = new JsonDeserializer<GetOrderListResponse>() {
		
		@Override
		public GetOrderListResponse deserialize(JsonElement json, Type arg1, JsonDeserializationContext context) throws JsonParseException {
			int exitCode = json.getAsJsonObject().get("exitCode").getAsInt();
			if (json.getAsJsonObject().get("result").isJsonArray()) {
				Order[] orders = context.deserialize(json.getAsJsonObject().get("result"), Order[].class);
				return new GetOrderListResponse(exitCode, orders);
			} else 
				return new GetOrderListResponse(exitCode, null);
		}
	};
}
